package com.xueke.crawler.service.initTree;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xueke.crawler.helper.HttpHelper;
import com.xueke.crawler.helper.HttpUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by weibo on 2017/5/13.
 *
 * @author weibo.qin
 * @version 2017/5/13
 */
@Component("otherInitTree")
public class OtherInitTree extends BaseInitTree{

    String domain;
    // 其他学科树形结构文档的api
    String childNodeUrl;
    private static Map<String, String> map = new HashMap<String, String>();

    static {
        map.put("yy", "http://yy.zxxk.com");
        map.put("hx", "http://hx.zxxk.com");
        map.put("sw", "http://sw.zxxk.com");

        map.put("zz", "http://zz.zxxk.com");
        map.put("ls", "http://ls.zxxk.com");
        map.put("dl", "http://dl.zxxk.com");
        map.put("kx", "http://kx.zxxk.com");
        map.put("zonghe", "http://zonghe.zxxk.com");
        map.put("zuhe", "http://zuhe.zxxk.com");
    }


    @Override
    public void start() {
        String[] keys="yy,hx,sw,zz,ls,dl,kx,zonghe,zuhe".split(",");

        //String[] keys="zonghe,zuhe".split(",");
        for(String key:keys){
            domain = map.get(key);
            childNodeUrl=domain + "/GetTreeNode.aspx/GetTreeNodeList";
            System.out.println(domain);
            doCrawler();
        }
        System.out.println("执行完毕");
    }


    private void doCrawler() {
        Document doc =HttpHelper.getSimpleDocument(domain + "/s-channel-12.html");

        Element tree = doc.getElementById("ClassTree");
        Element channel = tree.select("li:eq(0)").first();
        // System.out.println(tree);

        String channelId = parseChannelId(channel);
        String title = parseTitle(channel);
        String uri = parseUri(channel);
        String dataType = parseDataType(channel);
        int isLeaf = parseIsLeaf(channel);
        System.out.println(channelId + "-----" + title + "--------" + uri
            + "------" + isLeaf);
        saveSubjectTree(channelId, channelId, domain, uri, title, channelId,
            isLeaf, dataType);

        Elements lis = tree.select("li");

        for (int i = 1; i < lis.size(); i++) {
            Element li = lis.get(i);
            System.out.println(li);
            String dataId = parseChannelId(li);
            title = parseTitle(li);
            uri = parseUri(li);
            isLeaf = parseIsLeaf(li);
            dataType = parseDataType(li);
            int level = parseLevel(li);
            System.out.println(channelId + "-----" + title + "--------" + uri
                + "------" + isLeaf);

            saveSubjectTree(channelId, dataId, domain, uri, title, channelId,
                isLeaf, dataType);
            if (isLeaf == 0) {
                crawleTree(channelId, dataId, dataType, level);
            }
        }
    }

    private int parseLevel(Element li) {
        return Integer.parseInt(li.select("img").attr("level"));
    }



    private Object[] createInsertTreeObjects(String cid, String nid,
                                             String domain, String uri, String title, String pnid, int isDeaf,
                                             String dataType) {
        return new Object[] { nid, pnid, isDeaf, cid, title, domain, uri,
            dataType };
    }

    private void crawleTree(String channelId, String dataId, String dataType,
                            int level) {
        String url = getChildNodeUrl(channelId, dataId, dataType);
        String data = getPostData(dataType, dataId, level);
        System.out.println(url);
        System.out.println(data);
        String json = getJson(url, data);
        System.out.println(json);
        JSONObject result = JSONObject.parseObject(json);
        JSONArray array = result.getJSONArray("d");
        String _dataType = array.getString(2);
        JSONArray _array = JSONArray.parseArray(array.getString(1));

        for (int i = 0; i < _array.size(); i++) {
            JSONObject o = _array.getJSONObject(i);
            String _dataId = o.getString("NodeDataId");
            String _pid = dataId;
            String _title = o.getString("NodeName");
            String _uri = o.getString("NodeUrl").replaceAll("-0.html", ".html");
            int _isLeaf = o.getInteger("IsExistsChild") == 0 ? 1 : 0;

            System.out.println("_dataType=" + _dataType + ",_dataId=" + _dataId
                + ",_pid=" + _pid + ",_title=" + _title + ",_uri=" + _uri
                + ",_isLeaf=" + _isLeaf);

            saveSubjectTree(channelId, _dataId, domain, _uri, _title, _pid,
                _isLeaf, _dataType);
            if (_isLeaf == 0) {
                crawleTree(channelId, _dataId, _dataType, level + 1);
            }

        }

    }

    private String getPostData(String dataType, String dataId, int level) {
        JSONObject obj = new JSONObject();
        obj.put("ChannelID", "0");
        obj.put("ClassID", "0");
        obj.put("CharterID", "0");
        obj.put("SpecialID", "0");
        obj.put("Level", level + "");
        obj.put("UserKeyWords", "0");
        obj.put("ChannelClassID", "0");
        obj.put("PageType", "0");

        if (dataType.equals("class")) {
            obj.put("ClassID", dataId);
        } else {
            obj.put("CharterID", dataId);
        }

        return obj.toJSONString();
    }

    private String getJson(String url, String data) {
        return HttpUtil.doPost(url, data, "application/json; charset=UTF-8",
            "UTF-8");
    }

    private String getChildNodeUrl(String channelId, String dataId,
                                   String dataType) {
        return childNodeUrl.replace("${dataid}", dataId)
            .replace("${channelid}", channelId)
            .replace("${datatype}", dataType);
    }

    private String parseUri(Element tree) {
        return tree.select("a").attr("href").replaceAll("-0.html", ".html");
    }

    private String parseTitle(Element tree) {
        return tree.select("a").attr("title");
    }

    private String parseChannelId(Element tree) {

        return tree.select("img").attr("dataid");
    }

    private String parseDataType(Element tree) {

        return tree.select("img").attr("nodetype");
    }

    private int parseIsLeaf(Element li) {
        return li.select("img").attr("isload").equals("true") ? 1 : 0;
    }


}
